<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/5 0005
 * Time: 08:25
 */

namespace app\admin\controller;


use app\common\validate\IDMustBePostiveInt;
use think\Exception;
use think\facade\Request;
use app\common\model\Department as DepartmentModel;
use app\common\validate\DepartmentValidate;
use app\common\model\Staff as StaffModel;
use app\common\validate\StaffValidate;

class Staff extends Common
{
    /**
     * 部门列表
     * @return \think\response\Json|\think\response\View
     * @throws \think\exception\DbException
     */
    public function department() {
        if (Request::isAjax()) {
            $data = DepartmentModel::all();
            return return_data('部门列表', 0, $data);
        }
        return view();
    }

    /**
     * 保存部门信息
     * @return DepartmentModel|array|null|\think\response\View
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function save_department() {
        $id = input('param.id', 0);
        if (request()->isAjax()) {

            $department_validate = new DepartmentValidate();
            $department_validate->goCheck();
            $param = $department_validate->getDataByRule(input('param.'));
            $data = DepartmentModel::createOrUpdateDepartment($param, $id);
            return $data;
        } else {
            $data = [];
            if ($id > 0) {
                $data = DepartmentModel::get($id);
            }

            $this->assign('data', $data);

            return view();
        }
    }

    /**
     * 删除部门
     * @return \think\response\Json
     * @throws Exception
     * @throws \think\exception\DbException
     */
    public function delete_department() {
        (new IDMustBePostiveInt())->goCheck();
        $id = input('param.id');
        $department = DepartmentModel::get($id);
        if (!$department) {
            return return_data('部门不存在');
        }
        try {
            $department->delete();
            return return_data('删除成功！');
        } catch (Exception $e) {
            return return_data($e->getMessage(), 1);
        }
    }

    /**
     * 员工列表
     * @return \think\response\Json|\think\response\View
     * @throws \think\exception\DbException
     */
    public function staff_list() {
        if (Request::isAjax()) {
            $where = [];
            if (input('param.department_id', 0)) {
                $where['department_id'] = input('param.department_id');
            }
            $keyword = input('param.keyword', '');
            $data = (StaffModel::getList($where, $keyword))->toArray();
            return return_data('部门列表', 0, $data['data'], $data['total']);
        }
        return view();
    }

    /**
     * 保存员工信息
     * @return DepartmentModel|array|null|\think\response\Json|\think\response\View
     * @throws Exception
     * @throws \think\exception\DbException
     */
    public function save_staff() {
        $id = input('param.id', 0);
        if (request()->isAjax()) {

            $department_validate = new StaffValidate();
            $department_validate->goCheck();
            $param = $department_validate->getDataByRule(input('param.'));
            $data = StaffModel::createOrUpdateStaff($param, $id);
            return $data;
        } else {
            $data = [];
            if ($id > 0) {
                $data = StaffModel::get($id);
            }
            $this->assign('data', $data);

            // 所有部门
            $department = DepartmentModel::all();
            $this->assign('department', $department);

            return view();
        }
    }

    /**
     * 删除员工
     * @return \think\response\Json
     * @throws Exception
     * @throws \think\exception\DbException
     */
    public function delete_staff() {
        (new IDMustBePostiveInt())->goCheck();
        $id = input('param.id');
        $staff = StaffModel::get($id);
        if (!$staff) {
            return return_data('员工不存在');
        }
        try {
            $staff->delete();
            return return_data('删除成功！');
        } catch (Exception $e) {
            return return_data($e->getMessage(), 1);
        }
    }

    /**
     * 修改员工状态
     * @return \think\response\Json
     * @throws Exception
     * @throws \think\exception\DbException
     */
    public function save_staff_status() {
        (new IDMustBePostiveInt())->goCheck();
        $id = input('param.id');
        $staff = StaffModel::get($id);
        if (!$staff) {
            return return_data('员工不存在');
        }
        try {
            $staff->save([
                'status' => input('param.status')
            ]);
            return return_data('修改成功！');
        } catch (Exception $e) {
            return return_data($e->getMessage(), 1);
        }
    }
}